package yxy.game.pm2.command.godBeast;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PickedGodBeastMessage;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.godBeast.GodBeast;
import yxy.game.pm2.module.godBeast.PickedGodBeastHandle;
import yxy.game.pm2.module.godBeast.PickedGodBeastModule;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;

/**
 * @name CMD_PickedGodBeastEquipChangeOneButton
 * @author wrs
 * @description 神兽装备一键替换
 * @since 2022-05-31
 */
@MessageType(proto = Command.PickedGodBeastEquipChangeOneButton,classtype = PickedGodBeastMessage.PickedGodBeastEquipChangeOneButtonRequest.class)
public class CMD_PickedGodBeastEquipChangeOneButton extends Command<PickedGodBeastMessage.PickedGodBeastEquipChangeOneButtonRequest> {

    @Override
    public void execute(MessageSource messageSource, PickedGodBeastMessage.PickedGodBeastEquipChangeOneButtonRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int code = request.getCode();//神兽ID

        PickedGodBeastMessage.PickedGodBeastEquipChangeOneButtonResponse.Builder response = PickedGodBeastMessage.PickedGodBeastEquipChangeOneButtonResponse.newBuilder();

        try {
            PickedGodBeastModule module = PickedGodBeastModule.get(playerCode);
            if (PickedGodBeastModule.lockPickedGodBeast(playerCode)){
                PickedGodBeastHandle handle = new PickedGodBeastHandle();
                GodBeast godBeast = new PickedGodBeastHandle().getGodBeastById(module,code);
                if (godBeast == null){
                    send(messageSource,response, Result.ParamError);
                    return;
                }

                Result result = handle.changeEquipFast(playerCode,godBeast);
                if (!result.equals(Result.Success)){
                    send(messageSource,response,result);
                    return;
                }

                for (GodBeast value : module.getGbs().values()) {
                    response.addGbs(handle.getGodBeasts(playerCode, value));
                }

                send(messageSource,response,result);

                PlayTypeModule playTypeModule = PlayTypeModule.get(playerCode);
                PlayTypeHandle.savePickedGodBeast(playerCode,playTypeModule);
                new BattleHandle().powerUpdate(playerCode);// 战力更新

                //todo 神兽红点推送
            }
        }catch (Exception e){
            e.printStackTrace();
            send(messageSource,response, Result.DataError);
        }finally {
            PickedGodBeastModule.unlockPickedGodBeast(playerCode);
        }
    }

    private void send(MessageSource messageSource, PickedGodBeastMessage.PickedGodBeastEquipChangeOneButtonResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
